﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LEVBatteryPackControl
{

    public class LEVBPReceivedDataDetector
    {
        public enum RespondedDataType
        {
            None,
            Uart_Cmd_Transmit,
            Uart_Cmd_Respond,
            LEV_ST_G2_OneWire_SystemData,
            LEV_ST_G2_OneWire_EEPROM
        }
        public struct ReceivedPacket
        {
            public RespondedDataType Type;
            public byte Cmd;
            public byte[] Data;
        }
        List<byte> rawData;
        public LEVBPReceivedDataDetector()
        {
            rawData = new List<byte>();
        }

        public List<ReceivedPacket> DataDetector(byte[] appendRawData)
        {
            List<ReceivedPacket> detectorData = new List<ReceivedPacket>();
            //BatteryReceived_DataGroup group;
            byte respondCmd;
            byte[] receivedData;

            bool detectFlag = false;
            bool uartFound = false;
            int segStartIdxOfRawData = 0;
            int segEndIdxOfRawData = 0;
            int dataStartIdxOfRawData = 0;
            int dataEndIdxOfRawData = 0;
            ReceivedPacket packet;

            rawData.AddRange(appendRawData);

            do
            {
                detectFlag = false;
                uartFound = LEVUtilityLibrary.BatteryPack_UARTPacketFormat.Unpacking(
                            rawData.ToArray<byte>(),
                            out respondCmd,
                            out receivedData,
                            out segStartIdxOfRawData,
                            out segEndIdxOfRawData);
                if (uartFound)
                {
                    packet.Type = RespondedDataType.Uart_Cmd_Respond;
                    packet.Cmd = respondCmd;
                    packet.Data = receivedData;
                    detectorData.Add(packet);
                    rawData.RemoveRange(segStartIdxOfRawData, segEndIdxOfRawData - segStartIdxOfRawData + 1);
                    detectFlag = true;
                }
                LEVUtilityLibrary.BatteryPack_OneWirePacketFormat.OneWireDataGroup oneWireGroup =
                    LEVUtilityLibrary.BatteryPack_OneWirePacketFormat.UnPacking(
                                    rawData.ToArray<byte>(),
                                    out receivedData,
                                    out segStartIdxOfRawData,
                                    out segEndIdxOfRawData,
                                    out dataStartIdxOfRawData,
                                    out dataEndIdxOfRawData);
                if (oneWireGroup != LEVUtilityLibrary.BatteryPack_OneWirePacketFormat.OneWireDataGroup.None)
                {
                    detectFlag = true;
                    if (oneWireGroup == LEVUtilityLibrary.BatteryPack_OneWirePacketFormat.OneWireDataGroup.LEV_ST_G2_OneWire_EEPROM_Group)
                    {
                        packet.Type = RespondedDataType.LEV_ST_G2_OneWire_EEPROM;
                    }
                    else if (oneWireGroup == LEVUtilityLibrary.BatteryPack_OneWirePacketFormat.OneWireDataGroup.LEV_ST_G2_OneWire_SystemData_Group)
                    {
                        packet.Type = RespondedDataType.LEV_ST_G2_OneWire_SystemData;
                    }
                    else
                    {
                        throw new Exception("OneWire Data Group Detect Failure");
                    }
                    packet.Cmd = 0;
                    packet.Data = receivedData;
                    detectorData.Add(packet);
                    rawData.RemoveRange(0, segEndIdxOfRawData + 1);
                }
            } while (detectFlag);

            return detectorData;
        }

    }//public class LEVBPReceivedDataDetector
}//namespace LEVBatteryPackControl

